﻿namespace CYPlayer
{
    using System;
    using System.Windows;

    public partial class CompactWindow : Window
    {
        private PlaylistWindow playlist = null;

        public CompactWindow()
        {
            InitializeComponent();
        }

        private void MnModeBtnClick(object sender, RoutedEventArgs e)
        {
            var mainwin = new PlayerWindow();
            mainwin.Show();
            Close();
        }

        private void PLwinBtnClick(object sender, RoutedEventArgs e)
        {
            if (playlist == null)
            {
                playlist = new PlaylistWindow();
                playlist.Owner = this;
                playlist.Left = Left + Width;
                playlist.Top = Top + (Height - playlist.Height) / 2;
                Topmost = true;
                playlist.Topmost = true;
                playlist.Show();
                LocationChanged += new EventHandler(this.PlayerWindowLocationChanged);
                playlist.LocationChanged += new EventHandler(this.PlaylistWindowLocationChanged);
                playlist.Closed += new EventHandler(PlwinClosed);
            }
        }

        private void PlwinClosed(object sender, EventArgs e)
        {
            playlist = null;
        }

        void PlayerWindowLocationChanged(object sender, EventArgs e)
        {
            playlist.Left = Left + Width;
            playlist.Top = Top + (Height - playlist.Height) / 2;
        }

        private void PlaylistWindowLocationChanged(object sender, EventArgs e)
        {
            if (playlist.IsActive)
            {
                LocationChanged -= this.PlayerWindowLocationChanged;
            }
        }

        private void CompactWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (playlist != null)
            {
                playlist.Close();
            }
        }
    }
}
